package xyz.yuzegod.megawalls.command;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.config.FileConfig;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GameType;
import xyz.yuzegod.megawalls.game.team.TeamColor;
import xyz.yuzegod.megawalls.listener.SetupListener;

import java.util.Random;

public class CommandWalls extends BaseCommand
{
    private FileConfig config;
    
    public CommandWalls() {
        super("walls");
        this.setPermission("MegaWalls.admin");
    }
    
    @Override
    public String getPossibleArguments() {
        return "<setlobby/settype/setmapname/setup>";
    }
    
    @Override
    public int getMinimumArguments() {
        return 0;
    }
    
    @Override
    public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
        if (args.length == 0) {
            sender.sendMessage("§e§m------------------");
            sender.sendMessage("§f插件：§a" + MegaWalls.getInstance().getDescription().getName() + "§7v" + MegaWalls.getInstance().getDescription().getVersion());
            sender.sendMessage("§f作者：§a羽泽");
            sender.sendMessage("§e§m------------------");
            return;
        }
        if (MegaWalls.getInstance().getConfig().getBoolean("setup", false) || args.length == 0) {
            return;
        }
        if (args[0].equalsIgnoreCase("setup")) {
            if (Game.build(this.config, sender)) {
                sender.sendMessage("§a游戏安装成功!正在重启服务器!");
                MegaWalls.getInstance().getConfig().set("setup", (Object)true);
                MegaWalls.getInstance().getConfig().save();
                Bukkit.shutdown();
                return;
            }
            sender.sendMessage("§c游戏安装失败!");
        }
        else {
            if (args[0].equalsIgnoreCase("setlobby")) {
                final Location location = ((Player)sender).getLocation();
                this.config.setLocation(location, "lobby");
                this.config.save();
                sender.sendMessage("§a等待大厅设置成功!");
                return;
            }
            if (args[0].equalsIgnoreCase("settype")) {
                if (args.length >= 2) {
                    GameType type = null;
                    try {
                        type = GameType.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c类型仅有 DUEL 或 NORMAL!");
                        return;
                    }
                    this.config.set("type", (Object)type.name());
                    this.config.save();
                    sender.sendMessage("§a类型设置成功!");
                    return;
                }
                sender.sendMessage("§c/walls settype <c类型仅有DUEL/NORMAL>");
            }
            else if (args[0].equalsIgnoreCase("setmapname")) {
                if (args.length >= 2) {
                    this.config.set("mapname", (Object)args[1]);
                    this.config.save();
                    sender.sendMessage("§a成功设置地图名: " + args[1]);
                    return;
                }
                sender.sendMessage("§c/walls setmapname <mapname>");
            }
            else {
                if (args[0].equalsIgnoreCase("setspawn") && args.length >= 2) {
                    TeamColor teamColor = null;
                    try {
                        teamColor = TeamColor.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c队伍颜色错误(RED/YELLOW/BLUE/GREEN)");
                        return;
                    }
                    final Location location2 = ((Player)sender).getLocation();
                    this.config.setLocation(location2, "teams." + teamColor.name() + ".spawn");
                    this.config.save();
                    sender.sendMessage("§a队伍出生地设置成功!");
                    return;
                }
                if (args[0].equalsIgnoreCase("setwither") && args.length >= 2) {
                    TeamColor teamColor = null;
                    try {
                        teamColor = TeamColor.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c队伍颜色错误(RED/YELLOW/BLUE/GREEN)");
                        return;
                    }
                    final Location location2 = ((Player)sender).getLocation();
                    this.config.setLocation(location2, "teams." + teamColor.name() + ".wither");
                    this.config.save();
                    sender.sendMessage("§a队伍凋零出生地设置成功!");
                    return;
                }
                if (args[0].equalsIgnoreCase("setregion") && args.length >= 2) {
                    TeamColor teamColor = null;
                    try {
                        teamColor = TeamColor.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c队伍颜色错误(RED/YELLOW/BLUE/GREEN)");
                        return;
                    }
                    if (SetupListener.left == null || SetupListener.right == null) {
                        sender.sendMessage("§c请先选择区域!");
                        return;
                    }
                    this.config.setBlockLocation(SetupListener.left, "teams." + teamColor.name() + ".region.loc1");
                    this.config.setBlockLocation(SetupListener.right, "teams." + teamColor.name() + ".region.loc2");
                    this.config.save();
                    sender.sendMessage("§a队伍区域设置成功!");
                }
                else if (args[0].equalsIgnoreCase("setcastle") && args.length >= 2) {
                    TeamColor teamColor = null;
                    try {
                        teamColor = TeamColor.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c队伍颜色错误(RED/YELLOW/BLUE/GREEN)");
                        return;
                    }
                    if (SetupListener.left == null || SetupListener.right == null) {
                        sender.sendMessage("§c请先选择区域!");
                        return;
                    }
                    this.config.setBlockLocation(SetupListener.left, "teams." + teamColor.name() + ".castle.loc1");
                    this.config.setBlockLocation(SetupListener.right, "teams." + teamColor.name() + ".castle.loc2");
                    this.config.save();
                    sender.sendMessage("§a队伍城堡设置成功!");
                }
                else if (args[0].equalsIgnoreCase("setreserve") && args.length >= 2) {
                    TeamColor teamColor = null;
                    try {
                        teamColor = TeamColor.valueOf(args[1].toUpperCase());
                    }
                    catch (Exception e) {
                        sender.sendMessage("§c队伍颜色错误(RED/YELLOW/BLUE/GREEN)");
                        return;
                    }
                    if (SetupListener.left == null || SetupListener.right == null) {
                        sender.sendMessage("§c请先选择区域!");
                        return;
                    }
                    this.config.setBlockLocation(SetupListener.left, "teams." + teamColor.name() + ".reserve.loc1");
                    this.config.setBlockLocation(SetupListener.right, "teams." + teamColor.name() + ".reserve.loc2");
                    this.config.save();
                    sender.sendMessage("§a队伍保护区设置成功!");
                }
                else if (args[0].equalsIgnoreCase("setcenterarea")) {
                    if (SetupListener.left == null || SetupListener.right == null) {
                        sender.sendMessage("§c请先选择区域!");
                        return;
                    }
                    this.config.setBlockLocation(SetupListener.left, "center-area.loc1");
                    this.config.setBlockLocation(SetupListener.right, "center-area.loc2");
                    this.config.save();
                    sender.sendMessage("§a中心区设置成功!");
                }
                else {
                    if (!args[0].equalsIgnoreCase("addwall")) {
                        return;
                    }
                    if (SetupListener.left == null || SetupListener.right == null) {
                        sender.sendMessage("§c请先选择区域!");
                        return;
                    }
                    final int id = new Random().nextInt(100000);
                    this.config.setBlockLocation(SetupListener.left, "walls." + id + ".loc1");
                    this.config.setBlockLocation(SetupListener.right, "walls." + id + ".loc2");
                    this.config.save();
                    sender.sendMessage("§a墙添加成功!");
                }
            }
        }
    }
    
    @Override
    public boolean isOnlyPlayerExecutable() {
        return true;
    }
}
